package objects 
{
	import flash.geom.Point;
	import starling.display.Sprite;
	import starling.events.Event;
	/**
	 * ...
	 * @author Tuan
	 */
	public class SoliderManager 
	{
		private var m_pos : Point;
	    private var m_rallyPoint : Point;
		private var m_chooseEnemy : MyObject;
		private var m_isAttack : Boolean;
		public var m_listSolider : Array;
		private var m_layer : Sprite;
		
		private var m_arrayRespawn : Array;
		private var m_respawnTime1 : Number;
		private var m_respawnTime2 : Number;
		private var m_respawnTime3 : Number;
		
	    private var m_flag1 : Boolean;
		private var m_flag2 : Boolean;
		private var m_flag3 : Boolean;
		private var m_arrayFlag : Array;
		private var m_cooldown : Number; //get from database
		private var m_timeUpdate : Number;
		public var m_level :int ;
		public var m_bartrack : Bartrack;
		
		public function SoliderManager(_rallypoint : Point, _sprite : Sprite, _point :Point, level : int, _bartrack : Bartrack)
		{
			m_bartrack = null;
			m_bartrack = _bartrack;
			m_level =  level;
			m_pos = _point;
			m_layer = _sprite;
			m_rallyPoint = _rallypoint;
			m_isAttack = false;
			m_chooseEnemy =  null;
			m_listSolider = new Array();
			
			m_arrayRespawn = new Array();
			m_respawnTime1 = 0;
			m_respawnTime2 = 0;
			m_respawnTime3 = 0;
			m_arrayRespawn.push(m_respawnTime1);
			m_arrayRespawn.push(m_respawnTime2);
			m_arrayRespawn.push(m_respawnTime3);
			
			m_cooldown = Number (database.instance.RESULTSOLIDERARRAY[6]); //get from database;
			
			m_arrayFlag = new Array();
			m_arrayFlag.push (m_flag1);
			m_arrayFlag.push (m_flag2);
			m_arrayFlag.push (m_flag3);
			for (var i :int = 0; i < m_arrayFlag.length; i++)
			{
				m_arrayFlag[i] = false;
			}
		}
		
		public function update (dt : Number) : void
		{
	
		for ( var i: int = 0 ; i < m_listSolider.length ; i++) //linh chet di se duoc hoi sinh theo time
			{
				if (m_listSolider[i].m_hp <= 0)
				{
					if (m_listSolider.length == 3)
					{
				        m_arrayFlag[0] = true;
						
					}
					
					else if (m_listSolider.length == 2)
					{
				        m_arrayFlag[1] = true;
						
					}
					else if (m_listSolider.length == 1)
					{
				        m_arrayFlag[2] = true;
					
						
					}
					
				    
				}
			}
		
			for ( i = 0; i < m_arrayFlag.length; i++)
			{
			if (m_arrayFlag[i] == true)
			{
				m_arrayRespawn[i] += dt;
				
				if (m_arrayRespawn[i] >= m_cooldown)
				{
					m_arrayRespawn[i] = 0;
					m_arrayFlag[i] = false;
					if (this.m_listSolider.length == 0)
					{
						this.addSolider ( new Solider (m_layer, m_pos, new Point( m_rallyPoint.x + 30, m_rallyPoint.y)));	
					    this.getSolider(0).UpdateLevel(m_level);
						m_bartrack.m_animationState = Global.BARTRACK_OPEN;

					}
					else if (this.m_listSolider.length == 1)
					{
							this.addSolider ( new Solider (m_layer, m_pos, new Point( m_rallyPoint.x - 30, m_rallyPoint.y)));
							this.getSolider(1).UpdateLevel(m_level);
							m_bartrack.m_animationState = Global.BARTRACK_OPEN;
						
					}
					else if (this.m_listSolider.length == 2)
					{
						this.addSolider ( new Solider (m_layer, m_pos, new Point( m_rallyPoint.x  + 2, m_rallyPoint.y + 30 )));
					    this.getSolider(2).UpdateLevel(m_level);
						m_bartrack.m_animationState = Global.BARTRACK_OPEN;
					}
				}
			}
			}
			if (m_listSolider.length <= 0) // neu group chet het thi release target
			{
				m_chooseEnemy =  null;
				m_isAttack = false;
			}
			if (m_chooseEnemy != null && m_chooseEnemy.m_hp <= 0) 
			{
				m_chooseEnemy = null;
			}
			for ( i = 0 ; i < m_listSolider.length ; i++)
			{
				if (m_isAttack == true)// neu ca group deu k con doi' tuong, group di chuyen ve rallypoint 
				{
					if (m_listSolider[i].m_choosedEnemy != null) break;
					this.Move(m_rallyPoint);
					m_isAttack = false;
				}
			}
			
			for ( i = m_listSolider.length -1 ; i >= 0; i--)    //update and remove solider from array SoliderManager
			{
				m_listSolider[i].update (dt);
				if ( m_listSolider[i].m_hp  <= 0)
				{
						m_listSolider[i].release();
						m_layer.removeChild(m_listSolider[i]);
						m_listSolider.splice(i, 1);
						
						
				}
			}
			for (  i = 0 ; i < m_listSolider.length ; i++) 
			{
				if (m_chooseEnemy == null && m_listSolider[i].m_choosedEnemy != null )
				{

					m_chooseEnemy = m_listSolider[i].m_choosedEnemy ;
					
				}
			}
			for (  i = 0 ; i < m_listSolider.length ; i++) 
			{
				
					if (m_listSolider[i].m_choosedEnemy == null )
					{
						if (m_chooseEnemy != null)
						{
					    	m_isAttack = true;
						    m_listSolider[i].AttackWithNoColision( m_chooseEnemy, true);
						}
					}
			}
		}
		public function Collison ( _enemy : MyObject, dt: Number) : void
		{
			for ( var i: int = 0  ; i < m_listSolider.length ; i++)
			{
				m_listSolider[i].Collision(_enemy, dt);
			}
			
		}
		public function addSolider ( solider : Solider) : void
		{
			m_listSolider.push(solider);
		}
		public function Move (target : Point): void
		{
			for ( var i: int = 0  ; i < m_listSolider.length ; i++)
			{
				if ( i == 0)
				{
					var temp : Point = new Point(target.x + 30, target.y);
				}
				if ( i == 1)
				{
					temp  = new Point(target.x - 30, target.y);
				}
				if ( i == 2)
				{
					temp  = new Point(target.x + 2, target.y + 30);
				}
				m_listSolider[i].setTarget (temp);
			}	
			
		}
		public function setMoveByFlag ( point : Point) : void {
			
			m_rallyPoint = point;
			m_isAttack = false;
			
			if ( m_chooseEnemy != null )
			{
                 m_chooseEnemy = null;
			}
			
			
			for ( var i: int = 0 ; i < m_listSolider.length ; i++)
			{
				m_listSolider[i].m_moveByFlag = true;
			}
		}
		public function getSolider ( index : Number) : Solider
		{
			return m_listSolider[index];
		}
		public function getListSolider () : Array
		{
			return m_listSolider;
		}
		public function setDamage() :void { //use when upgrade
			
		}
		public function setHp() : void { //use when upgrade
			
		}
		public function setArmor():void { //use when upgrade
			
		}
		public function updateLevel(level : int) : void
		{
			for ( var i: int = 0 ; i < m_listSolider.length ; i++) //update level cho linh' o day
			{
				m_listSolider[i].UpdateLevel(level);
			}
		}
		public function release() : void
		{
			for ( var i : int = m_listSolider.length -1 ; i >= 0; i--)    //update and remove solider from array SoliderManager
			{
				    if (m_listSolider[i].m_choosedEnemy != null)//release target truoc khi release solider
					{

					    m_listSolider[i].m_choosedEnemy.m_countAttack -= 1;
					    m_listSolider[i].m_choosedEnemy.m_newAnimationState = Global.ENEMY_E_MOVE;
					    m_listSolider[i].m_choosedEnemy  = null;
					}
				    m_listSolider[i].m_hp = 0; 
					m_listSolider[i].release();
					m_layer.removeChild(m_listSolider[i]);
					m_listSolider.splice(i, 1);
			}
		}
		
		
	}

}